package com.onedec.designer.controller;


import com.onedec.designer.entity.OnedecFloorPlan;
import com.onedec.designer.entity.OnedecMaterial;
import com.onedec.designer.entity.OnedecMaterialExample;
import com.onedec.designer.entity.OnedecProjectDesignMaterials;
import com.onedec.designer.service.FileService;
import com.onedec.designer.service.OnedecFloorPlanService;
import com.onedec.designer.service.OnedecMaterialService;
import com.onedec.designer.service.OnedecProjectDesignMaterialsService;
import com.onedec.designer.util.Page;
import com.onedec.designer.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("materials")

public class OnedecMaterialsController {
    @Autowired
    private OnedecProjectDesignMaterialsService onedecProjectDesignMaterialsService;

@Autowired
    private OnedecMaterialService onedecMaterialService;

    @RequestMapping("selectAll")
    public Result select() throws Exception{
        List<OnedecMaterial> list = onedecMaterialService.selectAll();
    return new Result("200", "success", null,list);
    }

    @RequestMapping("changeCategory")
    public Result change(@RequestParam("materialCategory") String materialCategory ) throws Exception{

        List<OnedecMaterial> list = onedecMaterialService.selectByCategory(materialCategory);
        // List<OnedecMaterial> list = onedecMaterialService.selectAll();
        return new Result("200", "success", null,list);
    }

    @RequestMapping("changeName")
    public Result changeName(@RequestParam("materialName") String materialName ) throws Exception{

        List<OnedecMaterial> list = onedecMaterialService.selectByName(materialName);
        // List<OnedecMaterial> list = onedecMaterialService.selectAll();
        return new Result("200", "success", null,list);
    }
    @RequestMapping("changeModel")
    public Result changeName(@RequestParam("materialId") int  materialId ) throws Exception{

        OnedecMaterial onedecMaterial = onedecMaterialService.selectById(materialId);
        // List<OnedecMaterial> list = onedecMaterialService.selectAll();
        return new Result("200", "success", onedecMaterial,null);
    }

    @RequestMapping("query")
    public Result selectAll(OnedecProjectDesignMaterials onedecProjectDesignMaterials, Page page, int num) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("designProjectId", onedecProjectDesignMaterials.getDesignProjectId());
        map.put("designOperationNum", onedecProjectDesignMaterials.getDesignOperationNum());
        map.put("designStatus", onedecProjectDesignMaterials.getDesignStatus());
        map.put("pageIndex", page.getPageIndex());
        map.put("num", num);

        long dataCount = onedecProjectDesignMaterialsService.accountnum();

        page.setTotalCount(dataCount);
        page.setTotalPage((dataCount - 1) / num + 1);

        List<Map<String, Object>> maps = onedecProjectDesignMaterialsService.selectByMap(map);

        return new Result("200", "success", page, maps);
    }


    @RequestMapping("deleteAll")
    public Result getAway(@RequestParam(value = "nums[]") Integer[] nums) throws Exception {
        for (Integer i : nums) {
            onedecProjectDesignMaterialsService.deleteById(i);
        }
        return new Result("200", "success", null, null);
    }

    //单独删除一个商品
    @RequestMapping("delete")
    public Result delete(int id) throws Exception {
        onedecProjectDesignMaterialsService.deleteById(id);
        return new Result("200", "success", null, null);
    }

    //
    @RequestMapping("selectById")
    public Result selectById(int id) throws Exception {
        OnedecProjectDesignMaterials onedecProjectDesignMaterials = onedecProjectDesignMaterialsService.selectById(id);

        return new Result("200", "success", onedecProjectDesignMaterials, null);
    }

    //
    @RequestMapping("update")
    public Result update(@Validated OnedecProjectDesignMaterials onedecProjectDesignMaterials, BindingResult result) throws Exception {

        if (result.hasErrors()) {
            throw new RuntimeException("参数错误");
        }

        onedecProjectDesignMaterialsService.update(onedecProjectDesignMaterials);
        return new Result("200", "success", null, null);
    }

    @RequestMapping("selectByNum")
    public Result insert(String materialOperationNum) throws Exception {
        if(materialOperationNum.equals("0")){
            return new Result("200", "success", null, onedecMaterialService.selectAll());
        }else{
            OnedecMaterialExample example = new OnedecMaterialExample();
            OnedecMaterialExample.Criteria or = example.or();
            or.andMaterialOperationNumEqualTo(materialOperationNum);
            List<OnedecMaterial> list = onedecMaterialService.selectByNum(materialOperationNum);
            return new Result("200", "success", null, list);
        }

    }
    @RequestMapping("insert")

    public Result insert(@Validated OnedecProjectDesignMaterials onedecProjectDesignMaterials, BindingResult result) throws Exception {

        if (result.hasErrors()) {
            throw new RuntimeException("参数错误");
        }
        int id = onedecProjectDesignMaterials.getDesignMaterialsId();
        OnedecMaterial onedecMaterial = onedecMaterialService.selectById(id);
        onedecProjectDesignMaterials.setDesignOperationNum(onedecMaterial.getMaterialOperationNum());
        onedecProjectDesignMaterialsService.insertOne(onedecProjectDesignMaterials);
        return new Result("200", "success", null, null);
    }

    @PostMapping("insertAll")
    public Result insertAll(@RequestBody Map<Integer, String> map)throws Exception{
 for(Integer key:map.keySet()){//keySet获取map集合key的集合  然后在遍历key即可
     OnedecProjectDesignMaterials onedecProjectDesignMaterials = new OnedecProjectDesignMaterials();
     //todo 获取项目id
     onedecProjectDesignMaterials.setDesignProjectId(2);
     onedecProjectDesignMaterials.setDesignMaterialsId(key);
     onedecProjectDesignMaterials.setDesignQuantity(Integer.parseInt(map.get(key)));
     onedecProjectDesignMaterials.setDesignStatus("2");
     OnedecMaterial onedecMaterial = onedecMaterialService.selectById(key);
     onedecProjectDesignMaterials.setDesignOperationNum(onedecMaterial.getMaterialOperationNum());
     onedecProjectDesignMaterialsService.insertOne(onedecProjectDesignMaterials);
 }
        return new Result("200", "success", null, null);
    }
}
